package crp.core.course.domain.models;

import com.qwlabs.auditing.AuditedEntity;
import com.qwlabs.storage.models.StorageObject;
import crp.core.academic.domain.models.AcademicTermEntity;
import crp.core.department.domain.models.DepartmentEntity;
import crp.core.shared.PublicStatus;
import crp.core.shared.Tables;
import io.quarkiverse.hibernate.types.json.JsonTypes;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Type;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import java.util.Optional;

@Getter
@Setter
@Entity
@Table(name = Tables.COURSE_ATTRIBUTES)
public class CourseAttributesEntity extends AuditedEntity {
    @Id
    @GeneratedValue(generator = "UUID")
    @GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
    private String id;

    @OneToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "course_id", referencedColumnName = "id")
    private CourseEntity course;

    @ManyToOne(fetch = FetchType.LAZY, optional = true)
    private DepartmentEntity openDepartment;

    @ManyToOne(fetch = FetchType.LAZY, optional = true)
    private AcademicTermEntity academicTerm;

    @Enumerated(EnumType.STRING)
    private PublicStatus publicStatus;

    @Enumerated(EnumType.STRING)
    private CourseSourceType sourceType;

    @Type(type = JsonTypes.JSON_BIN)
    @Column(columnDefinition = JsonTypes.JSON_BIN)
    private StorageObject cover;

    public String getOpenDepartmentId() {
        return Optional.ofNullable(openDepartment).map(DepartmentEntity::getId).orElse(null);
    }

    public String getAcademicTermId() {
        return Optional.ofNullable(academicTerm).map(AcademicTermEntity::getId).orElse(null);
    }

    public String getCourseId() {
        return course.getId();
    }
}
